package cn.tkb.org.service.impl;

import cn.tkb.basic.service.impl.BaseServiceImpl;
import cn.tkb.org.domain.Department;
import cn.tkb.org.mapper.DepartmentMapper;
import cn.tkb.org.query.DepartmentQuery;
import cn.tkb.org.service.IDepartmentService;
import cn.tkb.basic.util.PageQuery;
import javafx.scene.Parent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;

    /**
     * 新增部门
     * @param department
     */
    @Override
    @Transactional
    public void add(Department department) {
        departmentMapper.save(department);//这里要返回自增长id
        this.update(department);
    }

    /**
     * 修改部门
     * @param department
     */
    @Override
    @Transactional
    public void update(Department department) {
        //设置dirPath
        String dirPath="";
        //如果新增的是顶级部门那么dirPath就是/+本身的id
        if(department.getParent()==null){
            dirPath="/"+department.getId();
        }else {//说明不是顶级部门
            //先找到上级部门的id根据id找到上级部门的dirPath
            Department parent = departmentMapper.loadById(department.getParent().getId());
            dirPath =parent.getDirPath()+"/"+department.getId();
        }
        department.setDirPath(dirPath);
        departmentMapper.update(department);
    }

    /**
     * 查询部门树只查询一级和二级
     * @return
     */
    @Override
    public List<Department> QeuryTree() {
        return departmentMapper.LoadTree();
    }

}
